package com.ruoyi.project.jiaqin.advertplatform.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.jiaqin.advertplatform.domain.JiaqinAdvertPlatform;
import com.ruoyi.project.jiaqin.advertplatform.service.IJiaqinAdvertPlatformService;
import com.ruoyi.project.jiaqin.news.domain.News;
import com.ruoyi.project.jiaqin.news.service.INewsService;
import com.ruoyi.project.system.member.domain.Member;
import com.ruoyi.project.system.member.service.IMemberService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 平台投放视频、文章广告Controller
 *
 * @author LCL
 * @date 2022-01-08
 */
@Controller
@RequestMapping("/jiaqin/advertplatform")
public class JiaqinAdvertPlatformController extends BaseController
{
    private String prefix = "jiaqin/advertplatform";

    @Autowired
    private IJiaqinAdvertPlatformService jiaqinAdvertPlatformService;
    @Autowired
    private INewsService newsService;
    @Autowired
    private IMemberService memberService;

    @RequiresPermissions("jiaqin:advertplatform:view")
    @GetMapping()
    public String advertplatform()
    {
        return prefix + "/advertplatform";
    }

    /**
     * 查询平台投放视频、文章广告列表
     */
    @RequiresPermissions("jiaqin:advertplatform:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JiaqinAdvertPlatform jiaqinAdvertPlatform)
    {
        startPage();
        List<JiaqinAdvertPlatform> list = jiaqinAdvertPlatformService.selectJiaqinAdvertPlatformList(jiaqinAdvertPlatform);
        return getDataTable(list);
    }

    /**
     * 导出平台投放视频、文章广告列表
     */
    @RequiresPermissions("jiaqin:advertplatform:export")
    @Log(title = "平台投放视频、文章广告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JiaqinAdvertPlatform jiaqinAdvertPlatform)
    {
        List<JiaqinAdvertPlatform> list = jiaqinAdvertPlatformService.selectJiaqinAdvertPlatformList(jiaqinAdvertPlatform);
        ExcelUtil<JiaqinAdvertPlatform> util = new ExcelUtil<JiaqinAdvertPlatform>(JiaqinAdvertPlatform.class);
        return util.exportExcel(list, "advertplatform");
    }

    /**
     * 新增平台投放视频、文章广告
     */
    @GetMapping("/add")
    public String add(Model model)
    {
        List<News> list = newsService.selectNewsList(new News());
        Member member = new Member();
        member.setIsCompany(0);
        List<Member> memberList = memberService.selectMemberList(member);
        model.addAttribute("newsList",list);
        model.addAttribute("memberList",memberList);
        return prefix + "/add";
    }

    /**
     * 新增保存平台投放视频、文章广告
     */
    @RequiresPermissions("jiaqin:advertplatform:add")
    @Log(title = "平台投放视频、文章广告", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JiaqinAdvertPlatform jiaqinAdvertPlatform)
    {
        JiaqinAdvertPlatform advertPlatform = jiaqinAdvertPlatformService.selectJiaqinAdvertPlatformByResId(jiaqinAdvertPlatform.getResId());
        if (advertPlatform != null) {
            return AjaxResult.error("该新闻已有广告");
        }
        jiaqinAdvertPlatformService.insertJiaqinAdvertPlatform(jiaqinAdvertPlatform);
        return AjaxResult.success();
    }

    /**
     * 修改平台投放视频、文章广告
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        JiaqinAdvertPlatform jiaqinAdvertPlatform = jiaqinAdvertPlatformService.selectJiaqinAdvertPlatformById(id);
        List<News> list = newsService.selectNewsList(new News());
        Member member = new Member();
        member.setIsCompany(0);
        List<Member> memberList = memberService.selectMemberList(member);
        mmap.put("newsList",list);
        mmap.put("memberList",memberList);
        mmap.put("jiaqinAdvertPlatform", jiaqinAdvertPlatform);
        return prefix + "/edit";
    }

    /**
     * 修改保存平台投放视频、文章广告
     */
    @RequiresPermissions("jiaqin:advertplatform:edit")
    @Log(title = "平台投放视频、文章广告", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JiaqinAdvertPlatform jiaqinAdvertPlatform)
    {
        JiaqinAdvertPlatform advertPlatform = jiaqinAdvertPlatformService.selectJiaqinAdvertPlatformByResId(jiaqinAdvertPlatform.getResId());
        if (advertPlatform != null && !advertPlatform.getId().equals(jiaqinAdvertPlatform.getId())) {
            return AjaxResult.error("该新闻已有广告");
        }
        jiaqinAdvertPlatformService.updateJiaqinAdvertPlatform(jiaqinAdvertPlatform);
        return AjaxResult.success();
    }

    /**
     * 删除平台投放视频、文章广告
     */
    @RequiresPermissions("jiaqin:advertplatform:remove")
    @Log(title = "平台投放视频、文章广告", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jiaqinAdvertPlatformService.deleteJiaqinAdvertPlatformByIds(ids));
    }
}
